package com.lantosec.hmrp.modules.supplier.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.supplier.entity.SupplierDir;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.supplier.entity.SupplierContract;
import com.lantosec.hmrp.modules.supplier.service.ISupplierContractService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierContractVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 供应商合同 控制器
 *
 * @author AutoCode
 * @since 2020-06-21
 */
@Api(value = "供应商合同控制器", tags = "供应商合同控制器")
@RestController
@RequestMapping("/supplier/contract")
@Log4j2
public class SupplierContractController extends BaseController {
    @Autowired
    private ISupplierContractService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部的供应商")
    public Result<List<SupplierDir>> all(@ApiParam(value = "供应商名称") @RequestParam(name = "supplier",required = false) String supplier){
        return Result.ok(service.getSupplierAll(supplier));
    }

    @GetMapping("saveSupplierDir")
    @ApiOperation(value = "新增供应商返回供应商编码")
    public Result<String> saveSupplierDir(@ApiParam(value = "供应商名称") @RequestParam(name = "supplier",required = false) String supplierName){
        String supplierCode = service.saveSupplierDir(supplierName);
        return Result.ok(supplierCode);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询供应商合同")
    public Result<Page<SupplierContractVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<SupplierContract> page,
                                                 @ApiParam(value = "供应商编码",required = true) @RequestParam(name = "supplierCode") String supplierCode){
        return Result.ok(service.getAll(page,supplierCode));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SupplierContractVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "供应商合同VO对象", required = true) @RequestBody @Validated SupplierContractVO vo){
        service.saveSupplierContract(vo);
        return Result.ok();
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "供应商合同VO对象", required = true) @RequestBody SupplierContractVO vo){
        service.updateSupplierContract(vo);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
